package com.swxc.smartorder.ui.base

import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.jaeger.library.StatusBarUtil
import com.swxc.smartorder.R
import com.swxc.smartorder.app.CommonAction
import com.swxc.smartorder.broadcast.NetworkChange
import com.swxc.smartorder.utils.ToastUtil


/**
 * Author   Administrator
 * Date     2018/3/29
 * Desc     eg
 *
 * @author Administrator
 */
open class BaseActivity : AppCompatActivity() {

    protected var intentFilter: IntentFilter? = null
    protected var networkChange: NetworkChange? = null
    protected lateinit var mContext: Context
    protected var BUG_TAG = this.javaClass.name

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mContext = this
        CommonAction.getInstance().addActivity(this)
        intentFilter = IntentFilter()
        intentFilter!!.addAction("android.net.conn.CONNECTIVITY_CHANGE")
        networkChange = NetworkChange()
        registerReceiver(networkChange, intentFilter)
        StatusBarUtil.setLightMode(this)
        StatusBarUtil.setColor(this, resources.getColor(R.color.ui_white), 0)
    }

    protected fun readyGo(clazz: Class<*>) {
        val intent = Intent(this, clazz)
        startActivity(intent)
    }


    protected fun readyGo(clazz: Class<*>, bundle: Bundle?) {
        val intent = Intent(this, clazz)
        if (null != bundle) {
            intent.putExtras(bundle)
        }
        startActivity(intent)
    }

    protected fun readyGoThenKill(clazz: Class<*>) {
        val intent = Intent(this, clazz)
        startActivity(intent)
        finish()
    }

    protected fun readyGoThenKill(clazz: Class<*>, bundle: Bundle?) {
        val intent = Intent(this, clazz)
        if (null != bundle) {
            intent.putExtras(bundle)
        }
        startActivity(intent)
        finish()
    }

    protected fun readyGoForResult(clazz: Class<*>, requestCode: Int) {
        val intent = Intent(this, clazz)
        startActivityForResult(intent, requestCode)
    }

    protected fun readyGoForResult(clazz: Class<*>, requestCode: Int, bundle: Bundle?) {
        val intent = Intent(this, clazz)
        if (null != bundle) {
            intent.putExtras(bundle)
        }
        startActivityForResult(intent, requestCode)
    }


    protected fun showToastSuccess(charSequence: CharSequence) {

        val toastUtil = ToastUtil(mContext, true, ToastUtil.STATUS_SUCCESS, charSequence as String?)
        toastUtil.show(2000)
    }

    protected fun showToastError(charSequence: CharSequence) {

        val toastUtil = ToastUtil(mContext, false, ToastUtil.STATUS_ERROR, charSequence as String?)
        toastUtil.show(2000)
    }

    protected fun showToastCommon(charSequence: CharSequence) {

        val toastUtil = ToastUtil(mContext, true, ToastUtil.STATUS_COMMON, charSequence as String?)
        toastUtil.show(2000)
    }


    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(networkChange)
    }
}
